package com.mhh.selary.backend.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;

/**
 * 请求入参类型
 *
 * @author mhh
 * @date 2020-09-17 17:24:42
 */
@Getter
@AllArgsConstructor
public enum MediaTypeEnum {

    /**
     * params:1, application/x-www-form-urlencoded:21, application/json:22, text/plain:31, multipart/form-data:4
     */
    PARAMS("Params", 1),

    APPLICATION_X_WWW_FORM_URLENCODED("application/x-www-form-urlencoded", 21),

    APPLICATION_JSON("application/json", 22),

    TEXT_PLAIN("text/plain", 31),

    MULTIPART_FORM_DATA("multipart/form-data", 4),

    ;

    private String media;

    private Integer type;

    public static MediaTypeEnum valueBy(Integer type) {
        return Arrays.stream(MediaTypeEnum.values()).filter(enumItem -> enumItem.getType().equals(type)).findFirst().orElse(null);
    }

    public static boolean isParam(Integer type) {
        if (PARAMS.getType().equals(type) || APPLICATION_X_WWW_FORM_URLENCODED.getType().equals(type) || MULTIPART_FORM_DATA.getType().equals(type)) {
            return true;
        }
        return false;
    }

    public static boolean isBody(Integer type) {
        if (APPLICATION_JSON.getType().equals(type) || TEXT_PLAIN.getType().equals(type)) {
            return true;
        }
        return false;
    }
}
